﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace PhanMemQuanLyCuaHangBanDienThoai
{
    public partial class Nha_Cung_Cap : Form
    {

        //Khai bao cac bien toan cuc
        DataSet ds;
        OleDbDataAdapter daNhaCC;
        
        public Nha_Cung_Cap()
        {
            InitializeComponent();
        }

        private void Nha_Cung_Cap_Load(object sender, EventArgs e)
        {
            ds = new DataSet();
            string strKetNoi = @"Provider=Microsoft.Jet.OleDB.4.0; Data Source=DTDD.mdb";
            string strSelect = @"SELECT * FROM NHACUNGCAP";
            string strInsert = @"INSERT INTO NHACUNGCAP(MaNCC, TenNCC, DiaChi, SDT) VALUES (?,?,?,?)";
            string strUpdate = @"UPDATE  NHACUNGCAP SET TenNCC=?,DiaChi=?,SDT=? WHERE MaNCC=?";
            string strDelete = @"DELETE FROM NHACUNGCAP WHERE  MaNCC=?";
            daNhaCC = new OleDbDataAdapter(strSelect, strKetNoi);

            //Hiển thị lên gridview
            daNhaCC.Fill(ds,"tblNHACUNGCAP");
            dgvNhaCungCap.DataSource = ds.Tables["tblNHACUNGCAP"];
            // Khoi tao cac doi tuong Command
            OleDbConnection con = new OleDbConnection(strKetNoi);
            // Doi tuong InsertCommand
            OleDbCommand comIns = new OleDbCommand(strInsert, con);
            comIns.Parameters.Add("@MaNCC", OleDbType.Integer, 4, "MaNCC");
            comIns.Parameters.Add("@TenNCC", OleDbType.WChar, 100, "TenNCC");
            comIns.Parameters.Add("@DiaChi", OleDbType.WChar, 20, "DiaChi");
            comIns.Parameters.Add("@SDT", OleDbType.Integer, 4, "SDT");
            daNhaCC.InsertCommand = comIns;
            // Doi tuong UpdateCommand
            OleDbCommand comup = new OleDbCommand(strUpdate, con);
            comup.Parameters.Add("@TenNCC", OleDbType.WChar, 100, "TenNCC");
            comup.Parameters.Add("@DiaChi", OleDbType.WChar, 20, "DiaChi");
            comup.Parameters.Add("@SDT", OleDbType.Double, 20, "SDT");
            comup.Parameters.Add("@MaNCC", OleDbType.Integer, 50, "MaNCC");
            daNhaCC.UpdateCommand = comup;
            // Doi tuong DeleteComand
            OleDbCommand comDel = new OleDbCommand(strDelete, con);
            comDel.Parameters.Add("@MaNCC", OleDbType.Integer, 4, "MaNCC");
            daNhaCC.DeleteCommand = comDel;
            //Hien thi du lieu
            
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            
            DataRow r = ds.Tables["tblNHACUNGCAP"].NewRow();
            r["MaNCC"] = txtMaNhaCungCap.Text;
            r["TenNCC"] = txtTenNhaCungCap.Text;
            r["DiaChi"] = txtDiaChi.Text;
            r["SDT"] = txtSoDienThoai.Text;
            ds.Tables["tblNHACUNGCAP"].Rows.Add(r);
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {

            daNhaCC.Update(ds, "tblNHACUNGCAP");
            MessageBox.Show("Luu thanh cong");
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            DataGridViewRow dr = dgvNhaCungCap.SelectedRows[0];

            // Remove dong duoc chon khoi datagridview
            dgvNhaCungCap.Rows.Remove(dr);
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
          
        }

        private void btnFirst_Click(object sender, EventArgs e)
        {
            
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            
        }

        private void dgvNhaCungCap_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
           
            

        }

        private void btnSua_Click(object sender, EventArgs e)
        {

        }

        private void dgvNhaCungCap_Click(object sender, EventArgs e)
        {
            DataGridViewRow dr = dgvNhaCungCap.SelectedRows[0];
            txtMaNhaCungCap.Text = dr.Cells[0].Value.ToString();
            txtTenNhaCungCap.Text = dr.Cells[1].Value.ToString();
            txtDiaChi.Text = dr.Cells[2].Value.ToString();
            txtSoDienThoai.Text = dr.Cells[3].Value.ToString();
        }
    }
}
